<template>
<el-dialog title="修改读者信息" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
        <el-form-item label="姓名" prop="name">
            <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
            <el-select v-model="dataForm.gender" placeholder="请选择">
                <el-option v-for="item in genderList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
            <el-input v-model="dataForm.idNo" placeholder="身份证号"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
            <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="手机号码"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
            <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
            <el-input v-model="dataForm.note" placeholder="备注"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    data() {
        return {
            genderList: [{
                    id: 1,
                    desc: "男"
                },
                {
                    id: 2,
                    desc: "女"
                }
            ],
            visible: false,
            dataForm: {
                id: '',
                name: '',
                gender: '',
                idNo: '',
                birthday: '',
                phone: '',
                email: '',
                note: ''
            },
            dataRule: {
                name: [{
                    required: true,
                    message: '姓名不能为空',
                    trigger: 'blur'
                }],
                gender: [{
                    required: true,
                    message: '性别不能为空',
                    trigger: 'blur'
                }],
                idNo: [{
                    required: true,
                    message: '身份证号不能为空',
                    trigger: 'blur'
                }],
                birthday: [{
                    required: true,
                    message: '出生日期不能为空',
                    trigger: 'blur'
                }],
                phone: [{
                    required: true,
                    message: '手机号码不能为空',
                    trigger: 'blur'
                }],
                email: [{
                    required: true,
                    message: '电子邮箱不能为空',
                    trigger: 'blur'
                }]
            }
        }
    },
    methods: {
        init(data) {
            this.dataForm = data
            this.visible = true
            // this.$nextTick(() => {
            //     this.$refs['dataForm'].resetFields()
            //     if (this.dataForm.id) {
            //         this.$http({
            //             url: this.$http.adornUrl(`/generator/treader/info/${this.dataForm.id}`),
            //             method: 'get',
            //             params: this.$http.adornParams()
            //         }).then(({
            //             data
            //         }) => {
            //             if (data && data.code === 0) {
            //                 this.dataForm.name = data.tReader.name
            //                 this.dataForm.gender = data.tReader.gender
            //                 this.dataForm.idNo = data.tReader.idNo
            //                 this.dataForm.birthday = data.tReader.birthday
            //                 this.dataForm.phone = data.tReader.phone
            //                 this.dataForm.email = data.tReader.email
            //                 this.dataForm.note = data.tReader.note
            //             }
            //         })
            //     }
            // })
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.$http({
                        url: this.$http.adornUrl(`/generator/treader/${!this.dataForm.id ? 'save' : 'update'}`),
                        method: 'post',
                        data: this.$http.adornData({
                            'id': this.dataForm.id || undefined,
                            'name': this.dataForm.name,
                            'gender': this.dataForm.gender,
                            'idNo': this.dataForm.idNo,
                            'birthday': this.dataForm.birthday,
                            'phone': this.dataForm.phone,
                            'email': this.dataForm.email,
                            'note': this.dataForm.note
                        })
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
